﻿using Microsoft.EntityFrameworkCore;
using System.Data;
using System.Linq.Expressions;
using ZenSystemService.Api.Entities;


namespace ZenSystemService.Api.Data
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
        {
        }

        public DbSet<User> Users { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<UserRole> UserRoles { get; set; }
        public DbSet<Institution> Institutions { get; set; }
        public DbSet<DeviceType> DeviceTypes { get; set; }
        public DbSet<InstitutionSalesOrderItem> InstitutionSalesOrderItems { get;set;}
        public DbSet<InstitutionSalesOrder> InstitutionSalesOrders { get; set; }
        public DbSet<InstitutionDevice> InstitutionDevices { get; set; }
        public DbSet<BusinessFunction> BusinessFunctions { get; set; }
        public DbSet<RoleFunction> RoleFunctions { get; set; }
        public DbSet<SMSVerificationCode> SMSVerificationCodes { get; set; }
        public DbSet<Area> Areas { get; set; }

        public DbSet<TencentAccessToken> TencentAccessTokens { get; set; }

        public DbSet<CourseType> CourseTypes { get; set; }
        public DbSet<Device> Devices { get; set; }
        public DbSet<Course> Courses { get; set; }
        public DbSet<CourseTrainee> CourseTrainees { get; set; }
        public DbSet<TrainingItem> TrainingItems { get; set; }
        public DbSet<CourseTrainingItem> CourseTrainingItems { get; set; }
        public DbSet<TraineeCourseTraining> TraineeCourseTrainings { get; set; }
        public DbSet<ProductType> ProductTypes { get; set; }
        public DbSet<Product> Products { get; set; }
        //public DbSet<ProductLabeled> ProductLabeleds { get; set; }
        public DbSet<TrainingItemProductType> TrainingItemProductTypes {  get; set; }

        public DbSet<TraineeCharacterRelation> TraineeCharacterRelations { get; set; }
        public DbSet<TraineeOrder> TraineeOrders { get; set; }
        public DbSet<TraineeOrderDetail> TraineeOrderDetails { get; set; }
        public DbSet<TraineeCourseSurplus> TraineeCourseSurpluss { get; set; }
        public DbSet<TrainingReport> TrainingReports { get; set; }
        public DbSet<LegencyUserZenUser> LegencyUserZenUsers { get; set; }

        public DbSet<RecommendTerm> RecommendTerms { get; set; }
        public DbSet<RecommendTermCoach> RecommendTermCoachs { get; set; }

        public DbSet<CancelReservationCourse> CancelReservationCourses { get; set; }

        public DbSet<TraineeOrderRecommendTermDetail> TraineeOrderRecommendTermDetails { get; set; }

        public DbSet<SubstituteCourseRecord> SubstituteCourseRecords { get; set; }

        public DbSet<ZenBrightTestOrder> ZenBrightTestOrders { get; set; }

        public DbSet<RecommendTermCase> RecommendTermCases { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            // 配置UserRole的复合键
            modelBuilder.Entity<UserRole>()
                .HasKey(ur => new { ur.UserId, ur.RoleId });

            // 配置User与UserRole的关系
            modelBuilder.Entity<UserRole>()
                .HasOne(ur => ur.User)
                .WithMany(u => u.UserRoles)
                .HasForeignKey(ur => ur.UserId);

            // 配置Role与UserRole的关系
            modelBuilder.Entity<UserRole>()
                .HasOne(ur => ur.Role)
                .WithMany(r => r.UserRoles)
                .HasForeignKey(ur => ur.RoleId);

            modelBuilder.Entity<ProductType>()
                .HasOne(pt => pt.DeviceType);
        }

        public IQueryable<T> FilterEntities<T>(IQueryable<T> query, string propertyName, object value)
        {
            // 确保propertyName是有效的属性名
            if (!typeof(T).GetProperty(propertyName)?.CanWrite ?? true)
            {
                throw new ArgumentException("Invalid property name");
            }

            // 创建参数表达式
            var parameter = Expression.Parameter(typeof(T), "x");
            var property = Expression.Property(parameter, propertyName);
            var valueConstant = Expression.Constant(value);

            // 创建比较表达式，例如: x => x.PropertyName == value
            var predicate = Expression.Lambda<Func<T, bool>>(
                Expression.Equal(property, valueConstant),
                parameter
            );

            return query.Where(predicate);
        }
    }
}
